#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>

#include <pthread.h>

#include "libretro.h"

#include "../include/helper/elf.h"
#include "../include/arm.h"
#include "../include/mrp.h"
#include "../include/utils/array.h"
#include "../include/utils/gzip.h"
#include "../include/utils/coding.h"
#include "../include/mrp/sys/fmt.h"

#define MRP_SCREEN_WIDTH 240
#define MRP_SCREEN_HEIGHT 320
#define MRP_SCREEN_PITCH (MRP_SCREEN_WIDTH * 2)

#define MRP_VM_SIZE (10 * 1024 * 1024)
#define MRP_MEM_SIZE (10 * 1024 * 1024)

static vm_info_t *mrp_vm;
static vm_config_t mrp_conf;

static retro_video_refresh_t video_cb = NULL;
static retro_input_poll_t poll_cb = NULL;
static retro_input_state_t input_cb = NULL;
static retro_audio_sample_batch_t audio_batch_cb = NULL;
static retro_environment_t environ_cb = NULL;

static struct retro_log_callback log_cb;

static pthread_mutex_t video_mutex;

static uint16 *video_buffer = NULL;
static int video_width, video_height, video_pitch;

void on_bufferRefresh(vm_info_t *vm, uint16 *bmp, int16 x, int16 y, uint16 w, uint16 h)
{
   pthread_mutex_lock(&video_mutex);
   uint16 *src_buf = bmp + x + y * w;
   uint16 *dst_buf = video_buffer + x + y * w;
   int i, j;
   for (i = 0; i < h; i++)
   {
      memcpy(dst_buf, src_buf, w * 2);
      src_buf += w;
      dst_buf += w;
   }
   pthread_mutex_unlock(&video_mutex);
}

void retro_set_environment(retro_environment_t cb)
{
   environ_cb = cb;
}

void retro_set_video_refresh(retro_video_refresh_t cb)
{
   video_cb = cb;
}

void retro_set_audio_sample(retro_audio_sample_t cb)
{
}

void retro_set_audio_sample_batch(retro_audio_sample_batch_t cb)
{
   audio_batch_cb = cb;
}

void retro_set_input_poll(retro_input_poll_t cb)
{
   poll_cb = cb;
}

void retro_set_input_state(retro_input_state_t cb)
{
   input_cb = cb;
}

void retro_init(void)
{
   pthread_mutex_init(&video_mutex, NULL);
}

void retro_deinit(void)
{
   pthread_mutex_destroy(&video_mutex);
}

unsigned retro_api_version(void)
{
   return RETRO_API_VERSION;
}

void retro_get_system_info(struct retro_system_info *info)
{
   info->need_fullpath = true;
   info->valid_extensions = "mrp";
#ifdef GIT_VERSION
   info->library_version = "(SVN)" GIT_VERSION;
#else
   info->library_version = "(SVN)";
#endif
   info->library_name = "mrpemu";
   info->block_extract = false;
}

void retro_get_system_av_info(struct retro_system_av_info *info)
{
   info->geometry.max_width = MRP_SCREEN_WIDTH * 2;
   info->geometry.max_height = MRP_SCREEN_HEIGHT * 2;
   info->geometry.base_width = MRP_SCREEN_WIDTH;
   info->geometry.base_height = MRP_SCREEN_HEIGHT;
   info->geometry.aspect_ratio = (float)MRP_SCREEN_WIDTH / (float)MRP_SCREEN_HEIGHT;
   info->timing.sample_rate = 48000;

   info->timing.fps = 60;
}

void retro_set_controller_port_device(unsigned port, unsigned device)
{
}

void retro_reset(void)
{
}

int pressed = 0;
void retro_run(void)
{
   printf("retro_run...");
   pthread_mutex_lock(&video_mutex);
   video_cb(video_buffer, MRP_SCREEN_WIDTH, MRP_SCREEN_HEIGHT, MRP_SCREEN_WIDTH * 2);
   pthread_mutex_unlock(&video_mutex);
   mr_event(mrp_vm, 0, 15, 0);
   mr_event(mrp_vm, 0, 6, 0);
   mr_event(mrp_vm, 1, 2, 0);
   mr_event(mrp_vm, 1, 4, 0);
}

size_t retro_serialize_size(void)
{
   return 0;
}

bool retro_serialize(void *data, size_t size)
{
   return false;

   if (size != retro_serialize_size())
      return false;

   return true;
}

bool retro_unserialize(const void *data, size_t size)
{
   return false;

   if (size != retro_serialize_size())
      return false;

   return true;
}

void retro_cheat_reset(void)
{
}

void retro_cheat_set(unsigned index, bool enabled, const char *code)
{
}

bool retro_load_game(const struct retro_game_info *info)
{
   const char *system_dir = NULL;
   enum retro_pixel_format rgb565 = RETRO_PIXEL_FORMAT_RGB565;

   if (environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &rgb565))
   {
   }

   if (environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &system_dir) && system_dir)
   {
   }

   const char *p = strrchr(info->path, '/');
   if (!p)
      p = strrchr(info->path, ':');
   if (!p)
      p = info->path;
   else
      p++;
   char entry[256];
   snprintf(entry, 256, "%%%s", p);

   if (video_buffer)
      free(video_buffer);
   video_buffer = calloc(1, MRP_SCREEN_WIDTH * MRP_SCREEN_HEIGHT * 2);

   //创建虚拟机
   mrp_vm = vm_create(MRP_VM_SIZE);

   mrp_conf.screenWidth = MRP_SCREEN_WIDTH;
   mrp_conf.screenHeight = MRP_SCREEN_HEIGHT;
   mrp_conf.networkId = 0;
   mrp_conf.sdcardDir = "ux0:data/";
   mrp_conf.dsmDir = "MrpEmuVita/";
   mrp_conf.IMEI = "IMEI";
   mrp_conf.IMSI = "IMSI";
   mrp_conf.manufactory = "YIZHI";
   mrp_conf.type = "MRPEMU";

   //设置一下虚拟机信息
   vm_setConfig(mrp_vm, &mrp_conf);

   mrp_vm->callbacks.onBufferRefresh = on_bufferRefresh;

   //启动mrp
   mr_start_dsmC(mrp_vm, entry);

   return true;
}

bool retro_load_game_special(unsigned game_type, const struct retro_game_info *info, size_t num_info)
{
   return false;
}

void retro_unload_game(void)
{
   vm_free(mrp_vm);
   if (video_buffer)
      free(video_buffer);
   video_buffer = NULL;
}

unsigned retro_get_region(void)
{
}

void *retro_get_memory_data(unsigned type)
{
   uint8_t *data;

   switch (type)
   {
   case RETRO_MEMORY_SAVE_RAM:
      break;
   case RETRO_MEMORY_SYSTEM_RAM:
      break;
   default:
      data = NULL;
      break;
   }

   return data;
}

size_t retro_get_memory_size(unsigned type)
{
   unsigned size;

   switch (type)
   {
   case RETRO_MEMORY_SAVE_RAM:
      break;
   case RETRO_MEMORY_SYSTEM_RAM:
      break;
   default:
      size = 0;
      break;
   }

   return size;
}
